package edu.gdin.ecommerce.action;


import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import edu.gdin.ecommerce.model.ParamDetail;
import edu.gdin.ecommerce.model.ParamType;
import edu.gdin.ecommerce.service.IParamService;
import edu.gdin.ecommerce.util.Pager;



@Scope("prototype")
@Controller("paramAction")
public class ParamAction extends ActionSupport {
	
	private IParamService paramService;
	
	private String name;
	private String typeDesc;
	private Integer currentPage;
	private Integer paramTypeId;
	
	private Pager pager;
	
	//用于存放返回信息
	private String messageReturn;

	
	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getTypeDesc() {
		return typeDesc;
	}


	public void setTypeDesc(String typeDesc) {
		this.typeDesc = typeDesc;
	}


	public IParamService getParamService() {
		return paramService;
	}
	

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	@Resource(name="paramService")
	public void setParamService(IParamService paramService) {
		this.paramService = paramService;
	}
	
	
	
	public String getMessageReturn() {
		return messageReturn;
	}


	public void setMessageReturn(String messageReturn) {
		this.messageReturn = messageReturn;
	}

	
	
	private String pdId;
	private String pdName;
	private String pdValue;
	private String pdDesc;
	
	
	public String getPdId() {
		return pdId;
	}


	public void setPdId(String pdId) {
		this.pdId = pdId;
	}


	public String getPdName() {
		return pdName;
	}


	public void setPdName(String pdName) {
		this.pdName = pdName;
	}


	public String getPdValue() {
		return pdValue;
	}


	public void setPdValue(String pdValue) {
		this.pdValue = pdValue;
	}


	public String getPdDesc() {
		return pdDesc;
	}


	public void setPdDesc(String pdDesc) {
		this.pdDesc = pdDesc;
	}

	
	private List<ParamType> allTypes;
	

	public List<ParamType> getAllTypes() {
		return allTypes;
	}


	public void setAllTypes(List<ParamType> allTypes) {
		this.allTypes = allTypes;
	}

	private Integer ptId;
	
	
	
	
	public Integer getPtId() {
		return ptId;
	}


	public void setPtId(Integer ptId) {
		this.ptId = ptId;
	}


	public Integer getParamTypeId() {
		return paramTypeId;
	}


	public void setParamTypeId(Integer paramTypeId) {
		this.paramTypeId = paramTypeId;
	}


	/**
	 * 用于分页查找系统参数类型
	 */
	public String queryParamTypeAction(){
			
		
		String tName = name==null ? "" : name.trim();
		String tDesc = typeDesc==null ? "" : typeDesc.trim();
		
		Integer cPage = currentPage;
		
		cPage = (cPage==null)? 1 : (cPage< 1 ? 1:cPage);
		
		Pager pager = paramService.queryParamType(tName, tDesc, cPage);
		
		setPager(pager); 
		setName(tName);
		setTypeDesc(tDesc);
		
		return "typeList";
	}
	
	/**
	 * 查询系统参数
	 * @return
	 */
	public String queryParamDetailAction(){
		
		
		String pId = pdId == null ? "" : pdId.trim();
		String pName = pdName == null ? "" : pdName.trim();
		String pValue = pdValue == null ? "" : pdValue.trim();
		String pDesc = pdDesc == null ? "" : pdDesc.trim();
		
		ParamDetail pDetail = new ParamDetail();
		pDetail.setParamDetailId(pId);
		pDetail.setName(pName);
		pDetail.setValue(pValue);
		pDetail.setDetailDesc(pDesc);
		
		Integer cPage = currentPage;
		
		cPage = (cPage==null)? 1 : (cPage< 1 ? 1:cPage);
		
		Pager pager = paramService.queryParamDetail(pDetail, cPage);
		
		setPager(pager);
		
		return "detailList";
	}
	
	
	public String addParamTypeAction(){
		
		if(name==null || !Pattern.matches("\\w+|[\u4e00-\u9fa5]+", name) || typeDesc==null || !Pattern.matches("\\w+|[\u4e00-\u9fa5]+", name))
			return "typeList";
		
		String tName = name.trim();
		String tDesc = typeDesc.trim();
		
		ParamType pType = new ParamType();
		pType.setName(tName);
		pType.setTypeDesc(tDesc);
		
		boolean isSuccess = paramService.addParamType(pType);
		
		if(isSuccess){
			setMessageReturn("success");
			return this.queryParamTypeAction();
		}else{
			this.addFieldError("error", "已存在相同名字!");
			return "typeAdd";
		}
	}
	
	
	public String queryAllTypes(){
		List<ParamType> allTypes = paramService.queryAllTypes();
		setAllTypes(allTypes);
		return "detailAdd";
	}
	
	public String addParamDetailAction(){
		
//			String strP = "^(\\w+|[\u4e00-\u9fa5]+)(:////)*(\\w+.|\\w+//)*(\\w+|[\u4e00-\u9fa5]+)$";
			
//			if(ptId==null || pdName==null || pdValue==null || pdDesc==null || !Pattern.matches(strP, pdId) || 
//					!Pattern.matches(strP, pdName) || !Pattern.matches(strP, pdValue) || !Pattern.matches(strP, pdValue))
//				return this.queryAllTypes();
//		
			
			ParamType pt = paramService.queryParamTypeById(ptId);
			
			ParamDetail pd = new ParamDetail();
			pd.setName(pdName.trim());
			pd.setParamDetailId(pdId.trim());
			pd.setValue(pdValue.trim());
			pd.setDetailDesc(pdDesc.trim());
			pd.setParamType(pt);
			
			boolean isSuccess = paramService.addParamDetail(pd);
			
			if(isSuccess){
				setMessageReturn("success");
				return queryParamDetailAction();
			}else{
				setMessageReturn("error");
				return this.queryAllTypes();
			}
		}
	
	public String queryDetailByTypeId(){
		Integer cPage = currentPage;
		
		cPage = (cPage==null)? 1 : (cPage< 1 ? 1:cPage);
		
		Pager pager = paramService.queryParamDetailByParamTypeId(paramTypeId, cPage);
		
		setPager(pager);
		
		return "detailList";
	}
}
